package com.dom.invoapp.modular.provider;

import com.baomidou.mybatisplus.plugins.Page;
import com.dom.api.sale.MarketBackApi;
import com.dom.common.constants.Constants;
import com.dom.common.model.standard.ResponseModel;
import com.dom.common.model.vo.sale.*;
import com.dom.invoapp.dao.entity.TmMarketBackFlow;
import com.dom.invoapp.dao.service.ITStorInfoService;
import com.dom.invoapp.modular.service.MarketBackService;
import com.xiaoleilu.hutool.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MarketBackProvider extends AbstractProvider implements MarketBackApi{

    @Autowired
    private MarketBackService marketBackService;
    @Autowired
    private ITStorInfoService storInfoService;
    @Override
    public String insertMarketBack(@RequestBody M050R request) {
        super.isValid(request);
        return marketBackService.insertMarketBack(request);
    }

    @Override
    public M059A getMarketBackList(@RequestBody M059R request) {
        Page<TmMarketBackFlow> page = marketBackService.getMarketBackList(request);
        M059A result = new M059A();
        result.setPs(page.getCurrent());
        result.setPn(page.getSize());
        result.setTotalNum((int)page.getTotal());
        result.setPages((int)page.getPages());
        List<SaleBackInfo> list = new ArrayList<SaleBackInfo>();
        Map<String,String> map = storInfoService.getStorInfoToMap();
        for (TmMarketBackFlow flow: page.getRecords()) {
            SaleBackInfo info = builBack(flow,map);
            list.add(info);
        }
        result.setBackInfoData(list);
        return result;
    }

    @Override
    public M053A findUpdateMarketBack(@RequestParam("id") String id) {
        return marketBackService.findUpdateMarketBack(id);
    }

    @Override
    public boolean marketBackDel(@RequestParam("id") String id) {
        return marketBackService.marketBackDel(id);
    }

    @Override
    public ResponseModel marketBackModify(@RequestBody M052R request) {
        return marketBackService.marketBackModify(request);
    }

    private SaleBackInfo builBack(TmMarketBackFlow flow,Map<String,String> map) {
//        TODO 属性赋值
        SaleBackInfo info = new SaleBackInfo();
        info.setId(flow.getId().toString());
        info.setBackType(flow.getBackType() == null ? null : flow.getBackType().toString());
        info.setStorCode(flow.getStorCode() == null ? null : flow.getStorCode().toString());
        info.setAddress(flow.getAddress());
        info.setSourPurcId(flow.getSourPurcId());
        info.setSourPurcType(flow.getSourPurcType());
        info.setSourSaleId(flow.getSourSaleId());
        info.setSourSaleType(flow.getSourSaleType());
        info.setStorName(flow.getStorCode() == null ? null : map.get(flow.getStorCode().toString()));
//        result.setCompCode(flow.getOrganCode() == null ? "" : flow.getOrganCode().toString());
        info.setCustName(flow.getCustName());
        info.setBackDate(flow.getBackDate() == null ? "" : DateUtil.formatDate(flow.getBackDate()));
        info.setExchDate(flow.getExchDate() == null ? "" : DateUtil.formatDate(flow.getExchDate()));
        info.setSuppCode(flow.getSuppCode() == null ? "" : flow.getSuppCode().toString());
        info.setSuppName(flow.getSuppName());
//        result.setSuppLinkTel(flow.g);
        info.setMakeDate(flow.getMakeDate() == null ? "" : DateUtil.formatDate(flow.getMakeDate()));
        info.setMakerId(flow.getDocuMakerId() == null ? "" : flow.getDocuMakerId().toString());
        info.setVerfyId(flow.getVerfierId() == null ? "" : flow.getVerfierId().toString());
//        info.setVerfyName(flow.getv);
        info.setVerfyDate(flow.getVerfyDate() == null ? "" : DateUtil.formatDate(flow.getVerfyDate()));
        info.setBillAbstract(flow.getDigest());
        info.setCancelStat(flow.getCancelStat() == null ? null : flow.getCancelStat().toString());
        info.setCancelTime(flow.getCancelTime() == null ? null : DateUtil.format(flow.getCancelTime(), Constants.DATE_FORMAT_YYYY_MM_DD));
//        info.setCompCode(flow.getOrganCode() == null ? null : flow.getOrganCode().toString());
        info.setCustCode(flow.getCustCode() == null ? null : flow.getCustCode().toString());
        info.setStat(flow.getStat().toString());
        info.setVerfyErrMsg(flow.getVerfyErrMsg());
        info.setVerfyStat(flow.getVerfyStat() == null ? null : flow.getVerfyStat().toString());
        return info;
    }
}
